package com.doudou.casebook.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.doudou.casebook.R;
import com.doudou.casebook.db.AccountBean;
import com.doudou.casebook.utils.GetTodayTime;

import java.util.Calendar;
import java.util.List;

public class AccountAdapter extends BaseAdapter {
    Context context;
    List<AccountBean> mDatds;
    LayoutInflater inflater;


    public AccountAdapter(Context context, List<AccountBean> mDatds) {
        this.context = context;
        this.mDatds = mDatds;
        inflater = LayoutInflater.from(context);


    }

    @Override
    public int getCount() {
        return mDatds.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatds.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_mainlv, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        AccountBean bean = mDatds.get(position);
        holder.typeIv.setImageResource(bean.getsImageId());
        holder.typeTv.setText(bean.getTypename());

        holder.beizhuTv.setText(bean.getBeizhu());
        holder.moneyTv.setText("¥"+bean.getMoney());

        if (bean.getYear() == GetTodayTime.year && bean.getMonth() == GetTodayTime.month && bean.getDay() == GetTodayTime.day) {
            String time = bean.getTime().split(" ")[1];

            holder.timeTv.setText("今天 "+time);
        }else {
            holder.timeTv.setText(bean.getTime());
        }


        return convertView;
    }

    class ViewHolder {
        ImageView typeIv;
        TextView typeTv, beizhuTv, timeTv, moneyTv;

        public ViewHolder(View view) {
            typeIv = view.findViewById(R.id.item_mainlv_iv);
            typeTv = view.findViewById(R.id.item_mainlv_tv_title);
            timeTv = view.findViewById(R.id.item_mainlv_tv_time);
            beizhuTv = view.findViewById(R.id.item_mainlv_tv_beizhu);
            moneyTv = view.findViewById(R.id.item_mainlv_tv_money);
        }
    }
}
